﻿using System;

namespace unTroublemaker
{
    public static class ExceptionExtension
    {
        private const string Newline = "\r\n";
        private const string NewlineX2 = Newline + Newline;
        private const string Tab = "\t";

        public static string Format(this Exception e)
        {
            string indent = string.Empty;
            string message = string.Empty;
            while (e != null)
            {

                message += indent + e.GetType().Name + ": " + e.Message + Newline;
                if(e.StackTrace != null)
                    message += indent + e.StackTrace.Replace(Newline, Newline + indent);

                indent += Tab;
                e = e.InnerException;
                message += NewlineX2;
            }

            return message;
        }
    }
}
